/*
 * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "It_posix_container.h"
#include "It_posix_queue.h"
#include <fstream>
#include <iostream>
using namespace std;

int const CHILD_FUNC_ARG = 0x2088;
#define STACK_SIZE (1024 * 1024)

static int childFunc(void *arg) {
    int ret;
    int value = *((int*)arg);
    ICUNIT_ASSERT_EQUAL(value, CHILD_FUNC_ARG, value);
    char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
    char mqname[MQUEUE_STANDARD_NAME_LENGTH] = "/testMQueue001";
    const char *msgptr = "childMsgs";
    struct mq_attr attr = { 0 };
    mqd_t mqueue;
    attr.mq_msgsize = 50;
    attr.mq_maxmsg = 255;

    mqueue = mq_open(mqname, O_RDWR | O_CREAT, 0644, &attr);
    ICUNIT_GOTO_NOT_EQUAL(mqueue, (mqd_t)-1, mqueue, EXIT1);

    ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
    ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);

    ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
    ICUNIT_GOTO_EQUAL(ret, strlen(msgptr), ret, EXIT1);
    ICUNIT_GOTO_STRING_EQUAL(msgrcd, msgptr, msgrcd, EXIT1);

EXIT1:
    if (mqueue >= 0) {
        ret = mq_close(mqueue);
        ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    }
    ret = mq_unlink(mqname);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    exit(7);
}

static UINT32 Testcase(void)
{
    uint32_t ret;
    int status;
    int exitCode;
    char *stack;
    char *stackTop;
    mqd_t mqueue;
    pid_t pid;
    int arg = CHILD_FUNC_ARG;
    stack = (char*)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
    stackTop = stack + STACK_SIZE;
    struct mq_attr attr = { 0 };
    attr.mq_msgsize = 50;
    attr.mq_maxmsg = 255;
    struct sigevent notification;
    notification.sigev_notify = 5;
    char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
    char mqname[MQUEUE_STANDARD_NAME_LENGTH] = "/testMQueue003";
    const char *msgptr = "parentMsgs";
    
    ret = unshare(CLONE_NEWIPC);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    mqueue = mq_open(mqname, O_RDWR | O_CREAT, 0644, &attr);
    ICUNIT_GOTO_NOT_EQUAL(mqueue, (mqd_t)-1, mqueue, EXIT2);
    
    ret = mq_notify(mqueue, &notification);
    ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT2);
    ICUNIT_GOTO_EQUAL(errno, EINVAL, errno, EXIT2);

    notification.sigev_notify = SIGEV_THREAD;
    ret = mq_notify(mqueue, &notification);
    ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT2);
    ICUNIT_GOTO_EQUAL(errno, ENOTSUP, errno, EXIT2);

    notification.sigev_notify = SIGEV_NONE;

    ret = mq_notify(-1, &notification);
    ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT2);
    ICUNIT_GOTO_EQUAL(errno, EBADF, errno, EXIT2);
    
    ret = mq_notify(mqueue, &notification);
    ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);

    memset(&attr, 0, sizeof(attr));

    ret = mq_getattr(mqueue, &attr);
    ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT2);

    attr.mq_flags |= O_NONBLOCK;

    ret = mq_setattr(mqueue, &attr, NULL);
    ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);

    ret = mq_getattr(mqueue, &attr);
    ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT2);

    ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
    ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);

    pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
    ICUNIT_GOTO_NOT_EQUAL(pid, -1, pid, EXIT2);

    ret = waitpid(pid, &status, 0);
    ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT2);

    ret = WIFEXITED(status);
    ICUNIT_GOTO_NOT_EQUAL(ret, 0, ret, EXIT2);

    exitCode = WEXITSTATUS(status);
    ICUNIT_ASSERT_EQUAL(exitCode, 7, exitCode);

    ret = mq_notify(mqueue, nullptr);
    ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);

    ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
    ICUNIT_GOTO_EQUAL(ret, MQUEUE_SHORT_ARRAY_LENGTH, ret, EXIT2);
    ICUNIT_GOTO_STRING_EQUAL(msgrcd, msgptr, msgrcd, EXIT2);

EXIT2:
    if (mqueue >= 0) {
        ret = mq_close(mqueue);
    }
EXIT3:
    ret = mq_unlink(mqname);
    return 0;
}

void ItPosixContainerIpcMq003(void)
{
    TEST_ADD_CASE("IT_POSIX_CONTAINER_IPC_MQ_003", Testcase, TEST_POSIX, TEST_CONTAINER, TEST_LEVEL0, TEST_FUNCTION);
}

